/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import Entity.Etbl_Foods;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author LeThuyLinh
 */
public class DALtbl_Foods extends BaseConnection{
    CallableStatement cstm=null;
    ResultSet rs=null;
     public boolean Addtbl_Food(Etbl_Foods obj){
        try {
            getConnection();
            cstm=cnn.prepareCall("{call tbl_Foods_add(?,?)}");
            cstm.setString("name", obj.getName());
            cstm.setString("description", obj.getDescription());
            cstm.execute();
            cstm.close();
            releaseConnection();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(DALtbl_Foods.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
    public boolean updatetbl_Food(Etbl_Foods obj){
        try {
            getConnection();
            cstm=cnn.prepareCall("{call tbl_Foods_update(?,?,?)}");
            cstm.setInt("foodId", obj.getFoodID());
            cstm.setString("name", obj.getName());
            cstm.setString("description", obj.getDescription());
            cstm.execute();
            cstm.close();
            releaseConnection();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(DALtbl_Foods.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
    public boolean deletetbl_Food(int foodID){
        try {
            getConnection();
            cstm=cnn.prepareCall("{call tbl_Foods_delete(?)}");
            cstm.setInt("foodId", foodID);
            cstm.execute();
            cstm.close();
            releaseConnection();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(DALtbl_Foods.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
     public Etbl_Foods getFood(int foodID){
        Etbl_Foods objFood=null;
        try {
            getConnection();
            cstm=cnn.prepareCall("{call tbl_Foods_get(?)}");
            cstm.setInt("foodId", foodID);
            rs=cstm.executeQuery();
            while(rs.next()){
                objFood=new Etbl_Foods();
                objFood.setFoodID(rs.getInt("foodId"));
                objFood.setName(rs.getString("name"));
                objFood.setDescription(rs.getString("description"));
                return objFood;
            }
            cstm.close();
            rs.close();
            releaseConnection();
        } catch (SQLException ex) {
            Logger.getLogger(DALtbl_Country.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    public ArrayList getListFood(String orderBy, String ASCorDESC, String WhereClause, int PagingIndex, int PagingSize){
        try {
            ArrayList foodList=new ArrayList();
            getConnection();
            cstm=cnn.prepareCall("{call tbl_Foods_getpaged(?,?,?,?,?)}");
            cstm.setString("OrderBy", orderBy);
            cstm.setString("TangGiam", ASCorDESC);
            cstm.setString("WhereClause", WhereClause);
            cstm.setInt("PageIndex", PagingIndex);
            cstm.setInt("PageSize", PagingSize);
            rs=cstm.executeQuery();
            DALTotalPageIndex.getTotalPage("tbl_Foods", WhereClause, PagingSize);
            while(rs.next()){
                Etbl_Foods objFood=new Etbl_Foods();
                objFood.setFoodID(rs.getInt("foodId"));
                objFood.setName(rs.getString("food_name"));
                objFood.setDescription(rs.getString("description"));
                foodList.add(objFood);
            }
            cstm.close();
            rs.close();
            releaseConnection();
            return foodList;
        } catch (SQLException ex) {
            Logger.getLogger(DALtbl_Cities.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}
